<template>
  <div>
    <div>
      <TopFilter ref="formPanel" :list="topFilterList" :cols="3" :labelWidth="90" @filterChange="formChangeHandle" />
      <FilterTable
        ref="table"
        columnsRef="jzyAddRecordModal"
        :columns="columns"
        :height="300"
        :dataSource="list"
        :isPagination="false"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      ></FilterTable>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="confirmDialog">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getXmcxzSelect, getZflmcSelect, getWxtypeSelect, getGongzSelect, getWxxmList, isWxxmCheck, addWxxmRecord } from '@/api/repair/business/customer';

export default {
  name: 'AddRecordModal',
  mixins: [dictionary],
  data() {
    this.selectedRows = [];
    return {
      topFilterList: this.creatTopFilterList(),
      columns: this.createTableColumns(),
      list: [],
      toperParams: {}
    };
  },
  mounted() {
    this.getXmcxzSelect();
    this.getZflmcSelect();
    this.getWxtypeSelect();
    this.getGongzSelect();
  },
  methods: {
    creatTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '项目车型组',
          fieldName: 'modelGroup',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '主分类名称',
          fieldName: 'primaryGroupName',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '维修项目代码',
          fieldName: 'LABOUR_CODE',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '项目维修类型',
          fieldName: 'REPAIR_TYPE_CODE',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '维修项目名称',
          fieldName: 'LABOUR_NAME',
          placeholder: '请输入...',
          change: val => {
            this.topFilterList[5].initialValue = val;
          }
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'SPELL_CODE',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '索赔代码',
          fieldName: 'OPERATION_CODE',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '工种',
          fieldName: 'WORKER_TYPE_CODE',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '总部标准工时',
          fieldName: 'OEM_LABOUR_HOUR',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '标准工时',
          fieldName: 'STD_LABOUR_HOUR',
          placeholder: '请输入...',
          change: val => {
            this.topFilterList[10].initialValue = Number(val);
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '派工工时',
          fieldName: 'ASSIGN_LABOUR_HOUR',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '索赔工时',
          fieldName: 'CLAIM_LABOUR',
          placeholder: '请输入...'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车型组',
          dataIndex: 'MODEL_LABOUR_CODE',
          minWidth: 200,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '车型组名称',
          dataIndex: 'MODEL_LABOUR_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否存在',
          dataIndex: 'has',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1278'),
          render: props => {
            return <span>{this.createDictText(props.row.has, '1278')}</span>;
          }
        }
      ];
    },
    async getXmcxzSelect() {
      const res = await getXmcxzSelect();
      this.topFilterList[0].itemList = res.map(x => ({ value: x.MODEL_LABOUR_CODE, text: x.MODEL_LABOUR_NAME }));
    },
    async getZflmcSelect() {
      const res = await getZflmcSelect();
      this.topFilterList[1].itemList = res.map(x => ({ value: x.MAIN_GROUP_CODE, text: x.MAIN_GROUP_NAME }));
    },
    async getWxtypeSelect() {
      const res = await getWxtypeSelect();
      this.topFilterList[3].itemList = res.map(x => ({ value: x.REPAIR_TYPE_CODE, text: x.REPAIR_TYPE_NAME }));
    },
    async getGongzSelect() {
      const res = await getGongzSelect();
      this.topFilterList[7].itemList = res.map(x => ({ value: x.WORKER_TYPE_CODE, text: x.WORKER_TYPE_NAME }));
    },
    async formChangeHandle(val) {
      this.toperParams = val;
      const res = await isWxxmCheck(val.modelGroup, val.LABOUR_CODE);
      if (res === '1') {
        return this.$notify({
          title: '警告',
          message: '已经存在',
          type: 'warning'
        });
      }
      this.list = await getWxxmList();
    },
    async saveData() {
      if (!this.selectedRows.length) {
        return this.$notify({
          title: '警告',
          message: '请选择需要复制的车型组!',
          type: 'warning'
        });
      }
      const MODEL_LABOUR_CODE = this.selectedRows.map(x => x.MODEL_LABOUR_CODE).join(',');
      const res = await addWxxmRecord({ ...this.toperParams, MODEL_LABOUR_CODE });
    },
    selectChangeHandle(rows) {
      this.selectedRows = rows;
    },
    async confirmDialog() {
      try {
        await this.saveData();
      } catch (e) {}
      this.closeDialog('reload');
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less"></style>
